<template>
  <div class="register-container">
    <el-card class="box-card">
      <h2 class="title"> <img src="../assets/BLOG.jpg" style="width: 50px;height: 50px;vertical-align: middle;" alt="">
        USER-BLOG</h2>

      <el-form :model="registerForm" :rules="rules" ref="registerForm" label-width="80px" class="register-form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="registerForm.password" placeholder="请输入密码"
            @keyup.enter.native="handleRegister"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="registerForm.confirmPassword" placeholder="请再次输入密码"
            @keyup.enter.native="handleRegister"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">

          <el-select v-model="registerForm.role" style="width: 100%;" placeholder="请选择角色">
            <el-option label="管理员" :value="0"></el-option>
            <el-option label="用户" :value="1"></el-option>


          </el-select>
        </el-form-item>
        <div class="tips" @click="login">已有账号？去登录~</div>
        <el-form-item>
          <el-button type="primary" @click="handleRegister" :loading="loading">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import service from '@/request';

export default {
  data() {
    return {
      registerForm: {
        username: '',
        password: '',
        confirmPassword: '',
  
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: this.validateConfirmPassword, trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'change' },
        ]
      },
      loading: false
    };
  },
  mounted() {

  },
  methods: {
 
    login() {
      this.$router.push({
        path: "/login"
      })
    },
    validateConfirmPassword(rule, value, callback) {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    },
    handleRegister() {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          delete this.registerForm.confirmPassword
          const res = await service.post("/user/register", this.registerForm);
          console.log(res);
   
if(res.code !== '0'){
  this.loading = false;
  this.registerForm={}
this.$message.warning(res.msg)
}else{
          // 这里应该加入实际的注册逻辑，例如调用 API 或者保存用户信息
          setTimeout(() => {
            this.loading = false;
            this.$message.success("注册成功")
            this.$router.push('/login');  // 注册成功后跳转到登录页面
          }, 1000);
}

        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.tips {
  width: 100%;
  text-align: right;
  font-size: 14px;
  color: #409EFF;
  margin: 10px 0px;
  cursor: pointer;
}

.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url("../assets/bg.png") no-repeat 100% 100%;
}

.box-card {
  width: 360px;
  padding: 30px;
  opacity: 0.9;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  margin-bottom: 20px;
  color: #303133;
}

.register-form {
  width: 100%;
}

.el-form-item__label {
  color: #303133;
}

.el-input {
  width: 100%;
}

.el-button {
  width: 100%;
}
</style>